<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>退回订单审核</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css">
    <style>.layui-table {
        table-layout: auto; /* 自动调整表格布局 */
        width: auto; /* 自动适应内容宽度 */
    }

    .layui-table-cell {
        white-space: nowrap; /* 防止内容换行 */
    }
    </style>

</head>
<body>
<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">审核</label>
                    <div class="layui-input-inline">
                        <select name="orderRollbackStatus">
                            <option value=0 selected>未审核</option>
                            <option value=1>审核通过</option>
                            <option value=2>审核拒绝</option>
                            <option value=4>查看所有</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>
<div class="layui-container1">
        <table class="layui-table" id="mytable" lay-filter="orderTable"></table>

</div>

<!-- 审核弹窗 -->

<script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.6.8/layui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js"></script>

<script>

    layui.use(['table', 'layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;



        // 自定义列模板：审核状态
        table.set({
            templet: {
                statusTpl: function(d){
                    if(d.auditStatus === 0){
                        return '<span style="color: red;">未审核</span>';
                    } else if(d.auditStatus === 1){
                        return '<span style="color: green;">审核通过</span>';
                    }
                    return '';
                }
            }
        });

        // 表格渲染
        var selectElement = document.querySelector('select[name="orderRollbackStatus"]');

// 获取当前选择的值
        var selectedValue = selectElement.value;
        console.log(selectedValue);

        table.render({
            elem: '.layui-table',
            url: 'http://localhost:8080/order/getRollbackDetailsBy101?status='+selectedValue,
            where: {
                status: selectedValue
            },
            method: 'GET',
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data.map(function(item) {
                        var order = item.orderFormVO101;
                        order.rollbackId = item.rollbackId;

                        order.orderRollbackStatus = item.status;
                        order.reason = item.reason;
                        return order;
                    })
                };
            },
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'userName', width: 70, title: '商家'},
                {field: 'orderClientname', width: 70, title: '姓名'},
                {field: 'productType', width: 120, title: '产品型号', sort: true},
                {field: 'orderDescription', width: 150, title: '订单描述'},
 //               {field: 'orderPriority', width: 100, title: '优先级', sort: true, templet: '#priorTpl'},
                {field: 'orderClientphone', width: 120, title: '用户手机号'},
                {field: 'orderDateTime', width: 160, title: '日期时间', templet: '#dateTimeTpl', sort: true},
                {field: 'orderArea', width: 80, title: '区县'},
                {field: 'orderAddress', width: 200, title: '详细地址'},
                {field: 'reason', width: 180, title: '退回原因'},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            limit: 10
        });


// 监听表单提交事件
        form.on('submit(data-search-btn)', function(data) {
            // 获取表单输入的值

            data = data.field.orderRollbackStatus;


            // var requestData = {
            //     auditName: name,
            //     auditPhone: phone,
            //     auditStatus: status
            // };
            console.log(data)

            // 重新加载表格数据
            table.reload('mytable', {
                url: 'http://localhost:8080/order/getRollbackDetailsBy101?status='+data, // 重新加载数据的 URL
                method: 'GET',
                page: { // 指定页码和每页显示的数据条数
                    curr: 1,
                    limit: 10
                },
                done: function(res, curr, count) {
                    // 重新加载完成后的回调函数
                    console.log('重新加载完成');
                }
            });
            // var tableData = layui.table.cache['mytable'];
            // console.log(tableData);
            return false;
        });






        // 监听表格行工具栏事件
        table.on('tool(orderTable)', function(obj){
            var data = obj.data;
            var rollbackId = data.rollbackId;
            if(obj.event === 'verify'){
                layer.confirm('审核通过？', function (index) {

                    $.ajax({
                        url: 'http://localhost:8080/order/changeRollbackStatus/'+ rollbackId + "?" + "status=1" ,
                        type: 'POST',
                        success: function(response) {
                            setTimeout(function (){
                                // Success callback
                                layer.msg('审核通过',{time: 300},function(){
                                    // Refresh the table
                                    table.reload('orderTable');
                                    obj.del();
                                });

                            })

                        },
                        error: function(xhr, status, error) {
                            // Error callback
                            console.error(error);
                            layer.msg('发生错误');
                        }
                    });

                    layer.close(index);


                })


            }else if(obj.event === 'reject'){
                layer.confirm('审核拒绝？', function (index) {

                    $.ajax({
                        url: 'http://localhost:8080/order/changeRollbackStatus/'+ rollbackId + "?" + "status=2" ,
                        type: 'POST',
                        success: function(response) {
                            setTimeout(function (){
                                // Success callback
                                layer.msg('审核拒绝',{time: 300},function(){
                                    // Refresh the table
                                    table.reload('orderTable');
                                    obj.del();
                                });

                            })

                        },
                        error: function(xhr, status, error) {
                            // Error callback
                            console.error(error);
                            layer.msg('发生错误');
                        }
                    });

                    layer.close(index);


                })
            }

        });

    });
    function formatTime(time) {
        if (time === 0) {
            return '上午';
        } else if (time === 1) {
            return '下午';
        } else if (time === 2) {
            return '晚上';
        } else {
            return '';
        }
    }
</script>



<script type="text/html" id="currentTableBar">
    {{# if(d.orderRollbackStatus === 1 || d.orderRollbackStatus === 2){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    {{# } else if(d.orderRollbackStatus === 0){ }}
    <a style="background-color: #16baaa; color:white" class="layui-btn layui-btn-primary  layui-btn-xs"  lay-event="verify">通过</a>
    <a style="background-color: #ba1616; color: white" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="reject">拒绝</a>
    {{# } else{ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    {{# } }}
</script>

<script>
    $(document).ready(function() {
        lightbox.option({
            'resizeDuration': 200,
            'wrapAround': true,
            'positionFromTop': 100
        });
    });

</script>
<script type="text/html" id="timeTpl">
    {{#  if(d.orderTime === 0){ }}
    上午
    {{#  } else if(d.orderTime === 1) { }}
    下午
    {{#  } else if(d.orderTime === 2) { }}
    晚上
    {{#  } }}
</script>
<script type="text/html" id="dateTimeTpl">
    {{ layui.util.toDateString(d.orderDate, 'yyyy-MM-dd') + ' ' + formatTime(d.orderTime) }}
</script>
<script type="text/html" id="priorTpl">
    {{#  if(d.orderPriority === 0){ }}
    正常
    {{#  } else if(d.orderPriority === 1) { }}
    较高
    {{#  } else if(d.orderPriority === 2) { }}
    高
    {{#  } else if(d.orderPriority === 3) { }}
    非常高
    {{#  } }}
</script>
</body>
</html>
